import {
  drawerFormState,
  merchantMap,
  orderStatusMap,
  payTypeMap,
  incomeTradeTypeMap,
  payTradeTypeMap,
  defaultDrawerDataSource,
  defaultTabsOptions,
  tableActiveKey,
  defaultOrderType,
} from "./store";
export const fetchIncomeSearchOptions = async () => {
  const res = reactive<any>(await tradeDetailApi.quertyIncomeSearchDicts());
  merchantMap.value = objectToOptions(res.data.merchantList, "number");
  incomeTradeTypeMap.value = [
    { label: $t("base.all"), value: Infinity },
    ...objectToOptions(res.data.orderTypeList, "number"),
  ];
  payTypeMap.value = [
    { label: $t("base.all"), value: Infinity },
    ...objectToOptions(res.data.platformList, "number"),
  ];
  initObjAttr(
    orderStatusMap.value,
    objectToOptions(res.data.statusList, "number")
  );
  initObjAttr(
    defaultTabsOptions[tableActiveKey.value],
    deepClone(
      [
        {
          label: $t("base.all"),
          value: 0,
        },
      ].concat(
        orderStatusMap.value.map(({ label, value }) => ({
          label,
          value: value + 1,
        }))
      )
    )
  );
};

export const fetchPaySearchOptions = async () => {
  const res = await tradeDetailApi.queryPaySearchDicts();
  merchantMap.value = objectToOptions(res.data.merchantList, "number");
  payTypeMap.value = [
    { label: $t("base.all"), value: Infinity },
    ...objectToOptions(res.data.platformList, "number"),
  ];
  initObjAttr(
    orderStatusMap.value,
    objectToOptions(res.data.statusList, "number")
  );
  initObjAttr(
    defaultTabsOptions[tableActiveKey.value],
    deepClone(
      [
        {
          label: $t("base.all"),
          value: 0,
        },
      ].concat(
        orderStatusMap.value.map(({ label, value }) => ({
          label,
          value: value,
        }))
      )
    )
  );
  payTradeTypeMap.value = [
    { label: $t("base.all"), value: Infinity },
    ...objectToOptions(res.data.orderTypeList, "number"),
  ];
};

const setFormState = (formState, data, record, number = 0) => {
  const setDefaultForm = () => {
    formState.order_number = data.order_number;
    formState.order_type = data?.order_type ?? record.order_type;
    formState.origin_type = data.origin_type;
    formState.third_order_number = data.third_order_number;
    if (number != 3) {
      formState.phone_number = data.phone_number;
      formState.user_info = data.user_info;
    }
    if (number != 3 && number != 1) {
      formState.pay_platform = data.pay_platform;
    }
    if (number === 4 || number === 0) {
      formState.status = data.status;
      formState.withdraw_time = data.withdraw_time;
      formState.arrival_time = data.arrival_time;
      formState.token_address = data.token_address;
      formState.withdraw_amount = data.withdraw_amount;
      formState.arrival_amount = data.arrival_amount;
      formState.handling_fee = data.handling_fee;
      formState.third_fee = data.third_fee;
      defaultDrawerDataSource[3][0] = data?.withdraw_level ?? {};
    }
  };
  const setForm1_2_3 = () => {
    formState.order_status = data.order_status;
    formState.create_time = data.create_time;
    formState.pay_type = data.pay_type;
  };
  const setForm1_3 = () => {
    formState.channel_name = data.channel_name;
  };
  const setForm2_3 = () => {
    formState.pay_time = data.pay_time;
  };

  const formMap: any = new Map([
    [/[123]/, setForm1_2_3],
    [/[13]/, setForm1_3],
    [/[23]/, setForm2_3],
    [/.*/, setDefaultForm],
  ]);
  const formEach = [...formMap].filter(([key]) => key.test(number));
  formEach.forEach(([, value]) => value.call(this));
};
export const fetchIncomeDetail = async (record) => {
  const res = await tradeDetailApi.queryIncomeDetail({
    order_id: record.id,
  });
  defaultOrderType[0] = incomeTradeTypeMap.value.find(
    (item) => item.label === record.order_type
  )?.value;
  const { data = {} } = res;
  setFormState(drawerFormState, data, record, defaultOrderType[0]);
  // 购买会员订单
  if (!isEmptyType(defaultOrderType[0]) && defaultOrderType[0] === 1) {
    drawerFormState.payment_time = data.payment_time;
    drawerFormState.member_info = data.member_info;
    defaultDrawerDataSource[0][0] = drawerFormState.member_info;
  }
  // 客户充值订单
  if (!isEmptyType(defaultOrderType[0]) && defaultOrderType[0] === 2) {
    drawerFormState.amount = data.amount;
    drawerFormState.actual_amount = data.actual_amount;
    defaultDrawerDataSource[1][0] = data.recharge_level;
  }
  // 购买红娘服务订单
  if (!isEmptyType(defaultOrderType[0]) && defaultOrderType[0] === 3) {
    drawerFormState.contract_info.sign_user_phone_number =
      data.contract_info.sign_user_phone_number;
    drawerFormState.contract_info.sign_user_info =
      data?.contract_info?.sign_user_info ?? {};
    drawerFormState.contract_info.send_user_info =
      data?.contract_info?.send_user_info ?? {};
    drawerFormState.contract_info.start_time = data.contract_info.start_time;
    drawerFormState.contract_info.contract_pdf =
      data.contract_info.contract_pdf;
    drawerFormState.contract_info.name = data.contract_info.name;
    drawerFormState.contract_info.attachment_file =
      data.contract_info.attachment_file;
    defaultDrawerDataSource[2][0] = {
      contract_no: data.contract_info.contract_no,
      name: data.contract_info.name,
      type: data.contract_info.type,
      amount: data.contract_info.amount,
      validity_period: data.contract_info.validity_period,
      end_time: data.contract_info.end_time,
      status: data.contract_info.status,
    };
  }

  // 提现手续费订单
  if (!isEmptyType(defaultOrderType[0]) && defaultOrderType[0] === 4) {
    drawerFormState.withdrawal_order_number = record.order_number;
  }
};

export const fetchPayDetail = async (record) => {
  const res = await tradeDetailApi.queryPayDetail({
    order_id: record.id,
  });
  const { data = {} } = res;
  setFormState(drawerFormState, data, record);
};
